#!/usr/bin/env python3



"""

This script takes in a list of usernames and runs queries for information about them: whether they are emailable and whether they have specified a gender


    Modified from send_email.py

    MediaWiki API Demos
    Demo of `Emailuser` module: sending POST request to send email to wiki user

    Author: Jayprakash12345
    MIT license
"""

import requests
import requests
import pandas as pd
import urllib
import time

# Step 0 - Globals and configs


#testing setup
#dataStub = 'test/'
#infile = 'testNames.tsv'
#outfile = 'namelookups_test.tsv'
#DEBUG = 1
#DEBUG = 0



#production setup
dataStub = '/data/users/kcz1100/taboo/StepWEmailable/'
infile = 'usernameList.tsv'
outfile = 'namelookups.tsv'


S = requests.Session()
S.headers.update({'User-Agent': 'Kaylea Champion querying user data, kaylea@uw.edu'})
check = 0



URL = "https://en.wikipedia.org/w/api.php"

names = []
data = pd.read_csv(dataStub + infile, sep='\t')   
data['editor'] = data['encodedEditor'].apply(lambda x: urllib.parse.unquote(x))
names = data['editor'].tolist()


with open(dataStub + outfile, 'w') as fh: #setup outfile
    fh.write(f'editor\tgender\temailable\n')


with open(dataStub + outfile, 'a') as fh: #setup outfile
    for name in names:
        name = name.replace('"', '')
        PARAMS = {"action": "query", 
            "format": "json",
            "list": "users",
            "usprop": "gender|emailable",
            "ususers": name,
            "maxlag": 30
            }

        if check == 50:
            time.sleep(1)
            check = 0
        check = check + 1
        request = S.get(url=URL, params=PARAMS)
        myData = request.json()
        result = myData["query"]["users"][-1]
        if 'gender' in result:
            gender = result["gender"]
        else:
            gender = 'unknown'
        if 'emailable' in result:
            emailable = "TRUE"
        else:
            emailable = "FALSE"
        fh.write(f'{name}\t{gender}\t{emailable}\n')



              ## Key query seems to be:://en.wikipedia.org/w/api.php?action=query&format=json&list=users&usprop=groups%7Ceditcount%7Cgender&ususers=Khascall

              #https://en.wikipedia.org/w/api.php?action=query&format=json&list=users&usprop=groups%7Cgender%7Cemailable&ususers=khascall
              #https://en.wikipedia.org/w/api.php?action=query&format=json&list=users&usprop=editcount%7Cgender%7Cemailable&ususers=Joejoe%20collins%2097%7CBlueraspberrybonbon%7Ckhascall

              #emailable seems to return "emailable": "" while non-emailable folks don't return that property despite being queried
